package com.changgou.pay.service.impl;

import com.changgou.pay.service.WxPayService;
import com.github.wxpay.sdk.WXPay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
@Service
public class WxPayServiceImpl implements WxPayService {

    @Autowired
    private WXPay wxPay;

    @Value( "${wxpay.notify_url}" )
    private String notifyUrl;

    @Override
    public Map nativePay(String orderId, Integer money) {
        Map<String,String> paramMap=new HashMap( 6 );
        paramMap.put( "body","畅购" );
        paramMap.put( "out_trade_no",orderId );
        paramMap.put( "total_fee",money+"" );
        paramMap.put( "spbill_create_ip","127.0.0.1" );
        paramMap.put( "notify_url",  notifyUrl );
        paramMap.put( "trade_type","NATIVE" );

        try {
            return wxPay.unifiedOrder( paramMap );
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Map queryOrder(String orderId) {
        Map<String,String> paramMap=new HashMap( 1 );
        paramMap.put( "out_trade_no",orderId );
        try {
            return wxPay.orderQuery( paramMap );
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 关闭订单
     *
     * @param orderId
     * @return
     */
    @Override
    public Map closeOrder(String orderId) {
        Map<String,String> paramMap=new HashMap( 1 );
        paramMap.put( "out_trade_no",orderId );
        try {
            return wxPay.closeOrder( paramMap );
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
